import {
  getOptions,
  getLanguage,
  getTenantOrganization,
  getBrand,
  getLogistics,
  getCurrency,
  getColumns,
  getWareData,
  getPlatform,
  getSupplie,
  getCountrys,
  getImporter,
  getImageTag,
  getAttributesData,
  getDepartment,
  getColumnsDepartment,
  getChannel,
  getImagesTagList,
  getCountryCurrency,
  queryDeclareElementList
} from '@/api/index'
import cache from '@/plugins/cache'

const params = {
  pageNum: 1, pageSize: 999
}
const useCommonStore = defineStore(
  'common',
  {
    state: () => ({
      options: [],
      languageOptions: '', // 语言列表
      personnelOptions: '', // 人员
      brandOption: '',  // 品牌
      logisticsOptions: '',  // 物流
      currencyOptions: '',  // 币种
      categoryOptions: '',  // 分类 （过滤禁用后）
      warehouseOptions: '', // 仓库
      platformOptions: '', //平台  22
      supplieOptions: '', //供应商
      shopOptions: [],
      countryOptions: "", // 国家
      importerOptions: "", // 进口商
      performanceTagOptions: "",  // 图片绩效标签
      taskTagOptions: "",  // 图片任务标签
      taskTypeOptions: "", // 图片任务类型
      taskTypebyUidOptions: "", // 图片任务类型 当前用户
      attributes_auth: "",  // 字段权限
      warehouseFilterOptions: '', // 过滤后仓库
      departmentOptions: '', // 部门
      columnDepartmentOptions: '', // 组织部门分类
      channelOptions: '', // 渠道列表
      countryCurrencyOptions: '', // 国家币种
      overseasWarehouseOptions: '', // 海外仓库
      declarationElementOptions: "", // 报关要素
    }),
    actions: {
      setOptions(key, value) {
        this.options[key] = value
      },
      getLanguageList() {
        return new Promise((resolve, reject) => {
          getLanguage({
            ...params,
            status: 1
          }).then(res => {
            this.languageOptions = res.data
            resolve()
          }).catch(error => {
            reject(error)
          })
        })
      },
      getOrganization() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getTenantOrganization()
            this.personnelOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      
      getBrandList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getBrand()
            this.brandOption = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getLogisticsList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getLogistics()
            // cache.session.setJSON('logistics', res.data)
            this.logisticsOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getCurrencyList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getCurrency()
            cache.session.setJSON('currency', res.data)
            this.currencyOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getColumnsList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getColumns()
            this.categoryOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getWarehouseList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getWareData({
              wms_attribute: [1,2,3,4,5,6]
            })
            this.warehouseOptions = res.data.map(i=>{
              return {
                label: i.warehouse_name,
                value: i.id,
              }
            })
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getWarehouseFilterList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getWareData()
            this.warehouseFilterOptions = res.data.filter(i => i.warehouse_name.includes('样品'));
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      
      getPlatformList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getPlatform()
            this.platformOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getSupplieList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getSupplie()
            this.supplieOptions = res.data.map(i => {
              return {
                label: i.supplier_name,
                value: i.s_id,
                purchase_type: i.purchase_type,
                purchase_type_name: i.purchase_type_name,
                supplier_tax_rate: i.supplier_tax_rate,
                finance_tax: i.finance_tax,
                supplier_code: i.supplier_code,
                is_invoice: i.is_invoice,
              }
            })
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getCountryList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getCountrys()
            console.log('mmmmm')
            console.log(res.data)
            this.countryOptions = res.data.map(i => {
              return {
                country_id: i.value,
                country_name: i.label,
              }
            })
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getImporterList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getImporter()
            this.importerOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getPerformanceTagList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getImageTag({
              userId: window.userInfo.user.user_id,
              tagType: 1
            })
            this.performanceTagOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getTaskTagList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getImageTag({
              userId: window.userInfo.user.user_id,
              tagType: 2
            })
            this.taskTagOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getTaskTypeList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getImagesTagList({
              tag_type: 4,
              pageSize: 1000
            })
            this.taskTypeOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getTaskTypebyUidList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getImageTag({
              userId: window.userInfo.user.user_id,
              tagType: 4
            })
            this.taskTypebyUidOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getAttributesList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getAttributesData({
              type: 3
            })
            this.attributes_auth = res.data.field_attr
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getDepartmentList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getDepartment()
            this.departmentOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getColumnsDepartmentList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getColumnsDepartment()
            this.columnDepartmentOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getColumnsDepartmentList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getColumnsDepartment()
            this.columnDepartmentOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getChannelList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getChannel({
              pageSize: 1000
            })
            this.channelOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getCountryCurrencyList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getCountryCurrency({
                pageSize: 1000,
                status: 1,
            })
            this.countryCurrencyOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getOverseasWarehouseList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getWareData({
              wms_attribute: [2,3,4]
            })
            this.overseasWarehouseOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },

      getDeclarationElementList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await queryDeclareElementList({
                pageSize: 3000,
                status: 1,
            })
            this.declarationElementOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      
    }
  })

export default useCommonStore
